<template>
  <el-row class="form-block">
    <el-row class="form-row-block">
      <el-col :span="2">权限描述：</el-col>
      <el-col :span="22">
        <el-input v-model="item.comment" size="mini"></el-input>
      </el-col>
    </el-row>
    <el-row class="form-row-block-title">
      <el-col :span="5">成员</el-col>
      <el-col :span="2">读</el-col>
      <el-col :span="2">写</el-col>
      <el-col :span="8">备注</el-col>
      <el-col :span="3">操作</el-col>
    </el-row>
    <el-row class="form-row-block" v-for="(auth, index) in item.items">
      <el-col :span="5">
        <el-select v-model="auth.name" size="mini" filterable style="width: 100%;">
          <el-option label="*" value="*"></el-option>
          <el-option v-for="(group, index) in groups.items" :label="'@' + group.name" :value="'@' + group.name">
          </el-option>
          <el-option v-for="(name, index) in users" :label="name" :value="name"></el-option>
        </el-select>
      </el-col>
      <el-col :span="2" style="padding-top: 5px;">
        <el-checkbox v-model="auth.read"></el-checkbox>
      </el-col>
      <el-col :span="2" style="padding-top: 5px;">
        <el-checkbox v-model="auth.write"></el-checkbox>
      </el-col>
      <el-col :span="8">
        <el-input v-model="auth.comment" size="mini"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button class="button-new-tag" size="mini" icon="el-icon-minus" @click="item.items.splice(index, 1)">移除
        </el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-button size="mini" icon="el-icon-plus" @click="item.items.push({name:'*',read:false,write:false})">添加
      </el-button>
      <el-button size="mini" icon="el-icon-check" @click="submit">提交</el-button>
    </el-row>
  </el-row>
</template>

<script>
  import access from '../api/access.js'
  export default {
    name: "authority",
    props: {
      item: {
        type: Object,
        require: true
      },
      users: {
        type: Array,
        required: true
      },
      groups: {
        type: Object,
        required: true
      }
    },
    methods: {
      submit: function() {
        this.item.items.forEach(e => {
          var temp = ''
          temp += (e.read ? 'r' : '')
          temp += (e.write ? 'w' : '')
          e.value = temp
        })
        access.updateAuthority(this.item).then(res => {
          if (!res.success) {
            this.$message.warning(res.message || '提交失败');
          }
        })
      }
    }
  }
</script>

<style>
</style>
